
$(function(){
	$("#goods_uploadList_button").swfupload(CTX,
		{	
        	button_width : 150,
        	button_height : 30,
        	button_text_left_padding : 60,
	        button_text_top_padding : 0.4,
	        button_text : "上传",
			upload_url : fileUploadUrl,
			progress_bar : $("#upload_progress ul"),
			file_dialog_complete_handler : function(countFile,countAddFile,countAllFile){
				//根据接口文档，定义所需 参数  
				var options ={
					"path": "/shop/"+shopId+"/goods/",
					"rename":"true"
				};
				this.setPostParams(options);
				//设定上传文件类型及说明
				var types = "*.jpg;*.gif;*.png";
				this.setFileTypes(types, "请选择");
				//设定上传文件体积,合法的单位有:B、KB、MB、GB，如果省略了单位，则默认为KB。该属性为0时，表示不限制文件的大小。
				this.setFileSizeLimit("2MB");
				this.startUpload();
			},
			upload_success_handler :function(file,data,response){
				var dataObj = eval('(' + data + ')');
				var dataPath="";
				if(dataObj.code == "1"){
						if(dataObj.files[0].repCode=="1"){
							dataPath = dataObj.files[0].path;
							$("#product_pic ul").append("<li><img src="+fileOpenUrl + dataPath+" id='img"+file.index+"' />" +
									"<div class='setNdelete'>" +
									"<span class='set_cover' title='设置默认' onclick="+"setDefPicForSrc('"+dataPath+"')"+"></span>" +
									"<span class='delete_pic' title='删除图片' onclick="+"delPic('img"+file.index+"','"+dataPath+"')"+"></span>" +
											"</div></li>");
							$("#img"+file.index).after("<input type='hidden' value='"+dataPath+"' name='goodsPicList["+file.index+"].imageUrl'></input>");
							picRoll();
							
						}else{
							alert("上传失败："+dataObj.files[0].repMsg);
						}
				}else{
					alert("文件上传失败："+dataObj.msg);
				}
			},
			upload_complete_handler :function(file){
				this.startUpload();
			}
			
		}
	);
	
	$("#upload_textarea_button").swfupload(CTX,
		{	
        	button_width : 150,
        	button_height : 30,
        	button_text_left_padding : 60,
	        button_text_top_padding : 0.4,
	        button_text : "上传",
			upload_url : fileUploadUrl,
			progress_bar : $("#upload_progress ul"),
			file_dialog_complete_handler : function(countFile,countAddFile,countAllFile){
				//根据接口文档，定义所需 参数  
				var options ={
					"path": "/shop/"+shopId+"/goods/",
					"rename":"true"
				};
				this.setPostParams(options);
				//设定上传文件类型及说明
				var types = "*.jpg;*.gif;*.png";
				this.setFileTypes(types, "请选择");
				//设定上传文件体积,合法的单位有:B、KB、MB、GB，如果省略了单位，则默认为KB。该属性为0时，表示不限制文件的大小。
				this.setFileSizeLimit("2MB");
				this.startUpload();
			},
			upload_success_handler :function(file,data,response){
				var dataObj = eval('(' + data + ')');
				var dataPath="";
				if(dataObj.code == "1"){
						if(dataObj.files[0].repCode=="1"){
							dataPath = dataObj.files[0].path;
							$("#text_area_pic ul").append("<li><div id='imgArea"+file.index+"'><img src="+fileOpenUrl + dataPath+" /></div>" +
									"<div class='setNdelete'>" +
									"<span class='set_cover'  onclick="+"insetEditor('imgArea"+file.index+"') title='加入编辑'></span>" +
											"</div></li>");
							picRoll_2();
							
						}else{
							alert("上传失败："+dataObj.files[0].repMsg);
						}
				}else{
					alert("文件上传失败："+dataObj.msg);
				}
			},
			upload_complete_handler :function(file){
				this.startUpload();
			}
			
		}
	);
});
/**
 * 初始化编辑器
 */

var goodsDetailEditor;
KindEditor.ready(function(K) {
	var option = {
			items : [
			 'source', '|', 'undo', 'redo', '|', 'preview',, 'template', 'cut', 'copy', 'paste',
			 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
			 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
			 'superscript', 'clearhtml', 'quickformat', '|', 'fullscreen', '/',
			 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
			 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
			 'anchor', 'link', 'unlink'	],
 			themeType : 'simple',
 			height :'350px',
 			allowFileManager : true,
 			filterMode:false
	};
	goodsDetailEditor = K.create('#goods_detail',option);
});

//将图片插入编辑器
function insetEditor(src){
	$("#"+src+" img ").css("max-width","100%");
	
	var srcHtml = $("#"+src).html();
	
	goodsDetailEditor.insertHtml(srcHtml);
}

//验证条形码
var barCodeExists;
function checkBarCodeVal(){
	var barCodeVal = $.trim($("#barCode").val());
	if(barCodeVal!=""){
		jQuery.ajax({
			url: CTX+"/shop/Merchandise/existsBarCode.do",
			type:"POST",
			data:"barCode="+barCodeVal,
			success:function(data){
				var result = data.resultExists;
				if(result=="true"){
					barCodeExists= true;
					Validator.showErrorTip(document.getElementById("barCode"),"条形码已存在");
				}else if(result=="false"){
					barCodeExists= false;
				}
			}
		});
	}
}
//验证表单
function checkForm(f){
	if(!Validator.Validate(f)){
		return false;
	}
	if(barCodeExists==true){
		return false;
	}
}
//删除图片
function delPicForId(picId){
	var merchandiseId = $.trim($("#merchandiseId").val());
	if(picId!=""){
		jQuery.ajax({
			url: CTX+"/shop/Merchandise/delPic.do",
			type:"POST",
			data:"goodsId="+merchandiseId+"&picId="+picId,
			success:function(data){
				if(data.resultDel=="1"){
					$("#"+picId).parent("li").remove();
				}else if(data.resultDel=="-1"){
					alert("默认图片请勿删除,否则请先配置其它为默认");
				}
			}
		});
	}
}

//删除商品图片
function delPic(Id,imgPath){
	var defImgPath = $("#default_pic input ").val();
	if(defImgPath == imgPath){
		alert("默认图片请勿删除");
	}else{
		$("#"+Id).parent("li").remove();
	}
}

//设置默认图片
function setDefPicForSrc(imgPath){
	$("#default_pic ").html("<p><img src="+fileOpenUrl + imgPath+"/></p> " +
			"<div class='setDefault'>默认图片</div></div>");
	$("#default_pic ").append("<input type='hidden' value='"+imgPath+"' name='defImgPath'></input>");
	
}
//查询二级分类
function findTypeForId(secId){
	$("#secTypeSelect [value!='']").remove();
	
	var parentId = $('#dUnitSelect option:selected').val();
	jQuery.ajax({
			url: CTX+"/shop/Merchandise/findTypeForId.do",
			type:"POST",
			data:"parentId="+parentId+"&shopId="+shopId,
			success:function(data){	
				for(var i in data){
					if(secId!=null&&data[i].merchandiseTypeId == secId){
						$("#secTypeSelect").append('<option selected="selected" value="'+data[i].merchandiseTypeId+'">'+data[i].merchandiseTypeName+'</option>');
					}else{
						$("#secTypeSelect").append('<option value="'+data[i].merchandiseTypeId+'">'+data[i].merchandiseTypeName+'</option>');
					}
				}
					
			}
		});
}